package com.shiroha.user.controller;

import com.shiroha.common.pojo.dto.GroupMemberDTO;
import com.shiroha.common.utils.Result;
import com.shiroha.user.pojo.GroupApplication;
import com.shiroha.user.pojo.dto.GroupApplicationDTO;
import com.shiroha.user.service.IGroupApplicationService;
import com.shiroha.user.service.IGroupMemberService;
import lombok.AllArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/us/groups")
@AllArgsConstructor
public class GroupMemberController {

    private final IGroupMemberService groupMemberService;
    private final IGroupApplicationService groupApplicationService;

    @GetMapping("/{groupId}/members")
    public List<GroupMemberDTO> getGroupMembers(@AuthenticationPrincipal Jwt jwt, @PathVariable("groupId") Long groupId) {
        return groupMemberService.getAllGroupMembersByGroupId(UUID.fromString(jwt.getSubject()), groupId);
    }

    // 发送入群申请
    @PostMapping("/{groupId}/apply")
    public Result<String> applyGroup(@AuthenticationPrincipal Jwt jwt, @PathVariable("groupId") Long groupId, @RequestBody String applyReason) {
        GroupApplicationDTO groupApplicationDTO = new GroupApplicationDTO();
        groupApplicationDTO.setGroupId(groupId);
        groupApplicationDTO.setUserId(UUID.fromString(jwt.getSubject()));
        groupApplicationDTO.setApplyReason(applyReason);

        return groupApplicationService.applyGroup(groupApplicationDTO) ?
                Result.success("发送入群申请成功") : Result.error("发送入群申请失败");
    }

    // 添加成员到群组中（无申请流程，直接添加）
    @PostMapping("/{groupId}/members")
    public Result<String> addGroupMemberToGroup(
            @AuthenticationPrincipal Jwt jwt,
            @PathVariable("groupId") Long groupId,
            @RequestParam UUID memberId
    ) {
        return groupMemberService.addGroupMemberToGroup(UUID.fromString(jwt.getSubject()), groupId, memberId) ?
                Result.success("添加成员成功") : Result.error("添加成员失败");
    }

    // 用户退出群聊
    @DeleteMapping("/{groupId}/members/leave")
    public Result<String> leaveGroup(@AuthenticationPrincipal Jwt jwt, @PathVariable("groupId") Long groupId) {
        return groupMemberService.leaveGroup(UUID.fromString(jwt.getSubject()), groupId) ?
                Result.success("退群成功") : Result.error("退群失败");
    }

    // 由管理员删除用户
    @DeleteMapping("/{groupId}/members")
    public Result<String> removeGroupMemberFromGroup(@AuthenticationPrincipal Jwt jwt, @PathVariable("groupId") Long groupId, @RequestParam UUID memberId) {
        return groupMemberService.deleteGroupMemberFromGroup(UUID.fromString(jwt.getSubject()), groupId, memberId) ?
                Result.success("删除成员成功") : Result.error("删除成员失败");
    }

    // 设置群管理员
    @PostMapping("/{groupId}/admin")
    public Result<String> setAdminForGroup(@AuthenticationPrincipal Jwt jwt, @PathVariable("groupId") Long groupId, @RequestParam UUID memberId) {
        return groupMemberService.setAdminForGroup(UUID.fromString(jwt.getSubject()), groupId, memberId) ?
                Result.success("创建群组成功") : Result.error("创建群组失败");
    }

    // 获取全部入群申请
    @GetMapping("/{groupId}/apply")
    public List<GroupApplication> getAllGroupApplications(@AuthenticationPrincipal Jwt jwt, @PathVariable("groupId") Long groupId) {
        return groupApplicationService.getAllGroupApplication(UUID.fromString(jwt.getSubject()), groupId);
    }

    // 处理入群申请
    @PutMapping("/{groupId}/apply")
    public Result<String> handleGroupApplication(
            @AuthenticationPrincipal Jwt jwt, @PathVariable("groupId") Long groupId,
            @RequestParam Long applicationId,  @RequestParam boolean isApproved
    ) {
        return groupApplicationService.handleApplication(UUID.fromString(jwt.getId()), groupId, applicationId, isApproved) ?
                Result.success("处理申请成功") : Result.error("处理申请失败");
    }
}
